package cz.hanis.android.pubaccounting.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;
import android.util.Log;

import cz.hanis.android.pubaccounting.db.DbConstants;
import cz.hanis.android.pubaccounting.db.tables.PubTable;
import cz.hanis.android.pubaccounting.db.tables.PubTable.PubColumns;
import cz.hanis.android.pubaccounting.model.Pub;

public class PubDao implements Dao<Pub> {

	private static final String INSERT = "insert into " + PubTable.TABLE_NAME
			+ "(" + PubColumns.NAME + ", " 
			+ PubColumns.ADDRESS + ", "
			+ PubColumns.HOMEPAGE + ", "
			+ PubColumns.LONGITUDE + ", " 
			+ PubColumns.LATITUDE
			+ ") values (?, ?, ?, ?, ?)";

	private SQLiteDatabase db;
	private SQLiteStatement insertStatement;

	public PubDao(SQLiteDatabase db) {
		this.db = db;
		insertStatement = db.compileStatement(PubDao.INSERT);
	}

	@Override
	public long save(Pub entity) {
		insertStatement.clearBindings();
		insertStatement.bindString(1, entity.getName());
		insertStatement.bindString(2, entity.getAddress());
		insertStatement.bindString(3, entity.getHompage());
		insertStatement.bindDouble(4, entity.getLongitude());
		insertStatement.bindDouble(5, entity.getLatitude());
		return insertStatement.executeInsert();
	}

	@Override
	public void update(Pub entity) {
		final ContentValues values = new ContentValues();
		values.put(PubColumns.NAME, entity.getName());
		values.put(PubColumns.ADDRESS, entity.getAddress());
		values.put(PubColumns.HOMEPAGE, entity.getHompage());
		values.put(PubColumns.LONGITUDE, entity.getLongitude());
		values.put(PubColumns.LATITUDE, entity.getLatitude());
		db.update(PubTable.TABLE_NAME, values, BaseColumns._ID + " = ?",
				new String[] { String.valueOf(entity.getId()) });
	}

	@Override
	public void delete(Pub entity) {
		if (entity.getId() > 0) {
			db.delete(PubTable.TABLE_NAME, BaseColumns._ID + " = ?",
					new String[] { String.valueOf(entity.getId()) });
		}
	}

	// get field slot error, show how it works by removing a column in the
	// columns array here)
	@Override
	public Pub get(long id) {
		Pub entity = null;
		Cursor c = db.query(PubTable.TABLE_NAME, new String[] {
				PubColumns._ID, PubColumns.NAME, 
				PubColumns.ADDRESS, PubColumns.HOMEPAGE,
				PubColumns.LONGITUDE, PubColumns.LATITUDE },
				PubColumns._ID + " = ?", new String[] { String.valueOf(id) },
				null, null, null, "1");
		if (c.moveToFirst()) {
			entity = this.buildEntityFromCursor(c);
		}
		if (!c.isClosed()) {
			c.close();
		}
		return entity;
	}

	@Override
	public List<Pub> getAll() {
		List<Pub> list = new ArrayList<Pub>();
		Cursor c = db.query(PubTable.TABLE_NAME, new String[] {
				PubColumns._ID, PubColumns.NAME,
				PubColumns.ADDRESS, PubColumns.HOMEPAGE, 
				PubColumns.LONGITUDE, PubColumns.LATITUDE},
				null, null, null, null, PubColumns.NAME, null);
		if (c.moveToFirst()) {
			do {
				Pub entity = this.buildEntityFromCursor(c);
				if (entity != null) {
					list.add(entity);
				}
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		return list;
	}


	private Pub buildEntityFromCursor(Cursor c) {
		Pub item = null;
		if (c != null) {
			item = new Pub();
			item.setId(c.getLong(0));
			item.setName(c.getString(1));
			item.setAddress(c.getString(2));
			item.setHompage(c.getString(3));
			item.setLongitude(c.getDouble(4));
			item.setLatitude(c.getDouble(5));
		}
		return item;
	}

}
